package com.kancy.jemeter.functions;

import com.kancy.jemeter.util.MessageUtil;
import com.kancy.jemeter.util.NumUtil;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;

import java.util.LinkedList;
import java.util.List;

/**
 * @author kancy
 * @date 2019/6/29 20:42
 */
public class NumFunction extends JemeterAbstractFunction {

	public NumFunction() {
		super(0, 4);
	}

	@Override
	public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {
		String format = getParameter(0, "#.00");
		String min = getParameter(1, "0");
		String max = getParameter(2, "99999999");
		String result = NumUtil.create(format, Double.parseDouble(min), Double.parseDouble(max));
		return setJMeterVariable(3,result);
	}

	private static final List<String> argumentDesclist = new LinkedList();

	static {
		argumentDesclist.add(MessageUtil.getResString("__num.format.message"));
		argumentDesclist.add(MessageUtil.getResString("__num.min.message"));
		argumentDesclist.add(MessageUtil.getResString("__num.max.message"));
		argumentDesclist.add(MessageUtil.getResString("varname.message"));
	}
	@Override
	public List<String> getArgumentDesc() {
		return argumentDesclist;
	}

}
